﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, Student> dic = new Dictionary<int, Student>();
            Student s1 = new Student(1, "张三", 20);
            Student s2 = new Student(2, "李四", 19);
            Student s3 = new Student(3, "王二", 18);
            dic.Add(s1.Num, s1);
            dic.Add(s2.Num, s2);
            dic.Add(s3.Num, s3);

            while (true)
            {
                Console.WriteLine("请选择：1、添加学生信息。2、查询学生信息。3、删除学生信息。4、退出");
                string choice = Console.ReadLine();

                switch (choice)
                {
                    case "1":

                        Add();
                        break;
                    case "2":

                        Select();
                        break;
                    case "3":

                        Delete();
                        break;
                    case "4":
                        Console.WriteLine("欢迎使用，再见!");
                        break;
                    default:

                        Console.WriteLine("输入错误，请重新输入");
                        break;
                }
            }


        }

        private static void Delete()
        {
            Dictionary<int, Student> dic = new Dictionary<int, Student>();
            Student s1 = new Student(1, "张三", 20);
            Student s2 = new Student(2, "李四", 19);
            Student s3 = new Student(3, "王二", 18);
            dic.Add(s1.Num, s1);
            dic.Add(s2.Num, s2);
            dic.Add(s3.Num, s3);
            Console.WriteLine("请输入学生学号");
            int num = int.Parse(Console.ReadLine());

            if (!dic.ContainsKey(num))
            {
                Console.WriteLine("学号不存在，请重新输入");
                return;
            }

            dic.Remove(num);
            Console.WriteLine("删除成功！");
        }

        private static void Select()
        {
            Console.WriteLine("请选择：1、查询所有（按学号排序），2、查询所有（按姓名排序），3、查询所有（按年龄排序），4、按学号查询 ，5、退出");
            string choice = Console.ReadLine();
            switch (choice)
            {
                case "1":
                    AllStudentNum();
                    break;
                case "2":
                    AllStudentName();
                    break;
                case "3":
                    AllStudentAge();
                    break;
                case "4":
                    StudentNum();
                        break;
                case "5":
                    Console.WriteLine("欢迎使用，再见!");
                    break;
                default:
                    Console.WriteLine("输入错误，请重新输入");
                    break;
            }
        }

        private static void StudentNum()
        {
            Dictionary<int, Student> dic = new Dictionary<int, Student>();
            Student s1 = new Student(1, "张三", 20);
            Student s2 = new Student(2, "李四", 19);
            Student s3 = new Student(3, "王二", 18);
            dic.Add(s1.Num, s1);
            dic.Add(s2.Num, s2);
            dic.Add(s3.Num, s3);
            Console.WriteLine("请输入学生学号");
            int num = int.Parse(Console.ReadLine());

            if (!dic.ContainsKey(num))
            {

                Console.WriteLine("查无此学生");

                return;
            }
            else
            {

                Console.WriteLine(dic[num]);

            }
        }

        private static void AllStudentAge()
        {
            Dictionary<int, Student> dic = new Dictionary<int, Student>();
            Student s1 = new Student(1, "张三", 20);
            Student s2 = new Student(2, "李四", 19);
            Student s3 = new Student(3, "王二", 18);
            dic.Add(s1.Age, s1);
            dic.Add(s2.Age, s2);
            dic.Add(s3.Age, s3);
            List<Student> list = new List<Student>();


            foreach (var value in dic.Values)
            {

                list.Add(value);
            }

            list.Sort(new SortAge());


            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }

        private static void AllStudentName()
        {
            Dictionary<string, Student> dic = new Dictionary<string, Student>();
            Student s1 = new Student(1, "张三", 20);
            Student s2 = new Student(2, "李四", 19);
            Student s3 = new Student(3, "王二", 18);
            dic.Add(s1.Name, s1);
            dic.Add(s2.Name, s2);
            dic.Add(s3.Name, s3);
            List<Student> list = new List<Student>();


            foreach (var value in dic.Values)
            {

                list.Add(value);
            }

            list.Sort(new SortName());


            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }

        private static void AllStudentNum()
        {
            Dictionary<int, Student> dic = new Dictionary<int, Student>();
            Student s1 = new Student(1, "张三", 20);
            Student s2 = new Student(2, "李四", 19);
            Student s3 = new Student(3, "王二", 18);
            dic.Add(s1.Num, s1);
            dic.Add(s3.Num, s3);
            dic.Add(s2.Num, s2);

            List<Student> list = new List<Student>();


            foreach (var value in dic.Values)
            {

                list.Add(value);
            }

            list.Sort(new SortNum());


            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }

        private static void Add()
        {
           Dictionary<int, Student> dic = new Dictionary<int, Student>();
            Student s1 = new Student(1, "张三", 20);
            Student s2 = new Student(2, "李四", 19);
            Student s3 = new Student(3, "王二", 18);
            dic.Add(s1.Num, s1);
            dic.Add(s2.Num, s2);
            dic.Add(s3.Num, s3);
            int num;

            while (true)
            { 
                
                Console.WriteLine("请输入学生学号");
                num = int.Parse(Console.ReadLine());

                if (dic.ContainsKey(num))
                {
                    Console.WriteLine("学号已存在，请重新输入");

                    continue;
                }

                break;
            }
            Console.WriteLine("请输入学生姓名");
            string name = Console.ReadLine();
            Console.WriteLine("请输入学生年龄");
            int age = int.Parse(Console.ReadLine());

            Student stu = new Student(num, name, age);

            dic.Add(num, stu);
            Console.WriteLine("添加成功!");
        }
    }
    }
